#include "Gui.h"

#include "Head.h"
static void Delete_Lvgl_Time(Time_Up *Time);
static void Create_Lvgl_Time(Time_Up *Time);
//定时器 刷数据、然后界面2删除
Time_Up Timer_Updata={
    .Updata_Timer=NULL,
    .Time_Period=500,
    .Timer_Delete=Delete_Lvgl_Time,
    .Timer_Create=Create_Lvgl_Time
};


float calibrate(float y)
{
    if      (y >= 6007) return 15430.0f + (y - 6007) * (15430 - 13820) / (6007 - 5416);
    else if (y >= 5416) return 13820.0f + (y - 5416) * (13820 - 12370) / (5416 - 4846);
    else if (y >= 4846) return 12370.0f + (y - 4846) * (12370 - 10616) / (4846 - 4249);
    else if (y >= 4249) return 10616.0f + (y - 4249) * (10616 - 9050 ) / (4249 - 3620);
    else if (y >= 3620) return 9050.0f  + (y - 3620) * (9050  - 7507 ) / (3620 - 3025);
    else if (y >= 3025) return 7507.0f  + (y - 3025) * (7507  - 5966 ) / (3025 - 2421);
    else if (y >= 2421) return 5966.0f  + (y - 2421) * (5966  - 4440 ) / (2421 - 1821);
    else if (y >= 1821) return 4440.0f  + (y - 1821) * (4440  - 2930 ) / (1821 - 1222);
    else if (y >= 1222) return 2930.0f  + (y - 1222) * (2930  - 1460 ) / (1222 - 627);
    else if (y >= 627 ) return 1460.0f  + (y - 627 ) * (1460  - 93   ) / (627  - 32);
    else                return 93.0f    + (y - 32  ) * (93    - 0    ) / (32   - 0);   // 外推到 0
}

//float calibrate_cct(float y)
//{
//    const float base = 4639.0f;
//    const float k    = -0.55f;
//    return base + k * (y - base);
//}



float calibrate_cct(float y)
{
    const double a =  2.34e-7;
    const double b = -3.18e-3;
    const double c =  14.02;
    const double d = -61720.0;
    double t = (y - 4589) / (4699 - 4589);   // 归一化 0..1
    return (float)(a*t*t*t + b*t*t + c*t + d);
}
int f;
int g;
static void Updata_Timer_Call_Back(lv_timer_t * timer)
{
    //这里刷新数据，定时，三种情况刷 单通道冷 单通道黄 双通道

        
    
//          if(C_Led.Light_Level&&(!W_Led.Light_Level))
//          {
//         lv_label_set_text_fmt(guider_ui.screen_label_lux_val, "%d",(int)Led_Channel_1.Cal_Lux(T3502_Val.Tsc_Val[Lux_Val],Led_Channel_1.Point_Lux_Copy,Led_Channel_1.Lux_Data));
//         lv_label_set_text_fmt(guider_ui.screen_label_cct_val, "%d",(int)Led_Channel_1.Cal_CCT(T3502_Val.Tsc_Val[CCT_Val],5,Led_Channel_1.Point_CCT_Copy,Led_Channel_1.CCT_Data));             
//          }
//          else if(W_Led.Light_Level&&(!C_Led.Light_Level))
//          {
//         lv_label_set_text_fmt(guider_ui.screen_label_lux_val, "%d",(int)Led_Channel_2.Cal_Lux(T3502_Val.Tsc_Val[Lux_Val],Led_Channel_2.Point_Lux_Copy,Led_Channel_2.Lux_Data));
//         lv_label_set_text_fmt(guider_ui.screen_label_cct_val, "%d",(int)Led_Channel_2.Cal_CCT(T3502_Val.Tsc_Val[CCT_Val],5,Led_Channel_2.Point_CCT_Copy,Led_Channel_2.CCT_Data));             
//          }
//          else
//          {
//         lv_label_set_text_fmt(guider_ui.screen_label_lux_val, "%d",(int)T3502_Val.Tsc_Val[Lux_Val]);
//         lv_label_set_text_fmt(guider_ui.screen_label_cct_val, "%d",(int)T3502_Val.Tsc_Val[CCT_Val]);              
//          } 
//         lv_label_set_text_fmt(guider_ui.screen_label_x_val, "%d",(int)T3502_Val.Tsc_Val[X_Val]);
//         lv_label_set_text_fmt(guider_ui.screen_label_y_val, "%d",(int)T3502_Val.Tsc_Val[Y_Val]);
//         lv_label_set_text_fmt(guider_ui.screen_label_z_val, "%d",(int)T3502_Val.Tsc_Val[CCT_Val]);

}

static void Create_Lvgl_Time(Time_Up *Time)
{
    if(Time->Updata_Timer==NULL)
    Time->Updata_Timer=lv_timer_create(Updata_Timer_Call_Back, Time->Time_Period, NULL);
    else return;
}

static void Delete_Lvgl_Time(Time_Up *Time)
{
    if(Time->Updata_Timer!=NULL)
    lv_timer_del(Time->Updata_Timer);
    Time->Updata_Timer=NULL;
}

